<?php
namespace app\admin\controller;

use app\admin\model\SystemPc as SystemModel;
use app\admin\model\SystemMobile;
use think\Request;
use think\Config;
use think\Cache;
use think\Validate;

class System extends Base
{
    public function about()
    {
        if(Request::instance()->isPost()){
            $data = $this->request->param();
            // dd($data);
            $validate = new Validate([
                ['title', 'require', '请输入标题'],
                ['title_sub', 'require', '请输入小标题'],
                ['content', 'require', '请输入详细内容'],
            ]);
            if (!$validate->check($data)) {
                return error($validate->getError());
            }
            $res = \think\DB::table('d_abouts')->where('id', $data['id'])->update($data);

            if($res || $res !== false){
                logger(2, "编辑更新关于我们");

                return success(1, '更新成功');
            }

            return error('更新失败');
        }

        $menu = self::getMenuArr();
        $detail = \think\Db::table('d_abouts')->order('id DESC')->find();

        return view('about', compact('menu', 'detail'));
    }

    //一键清空系统日志
    public function syslog_del()
    {
        $res = \think\Db::table('d_admin_log')->where('id','>=', 1)->delete();

        if($res){
            logger(2, '清空系统日志');
            
            return success(['url'=>"/admin/system/syslog"], '系统日志已清空');
        }

        return error('操作失败');
    }

    //系统日志
    public function syslog()
    {
        $map = [];
        if (!empty($this->request->param('keywords'))) {
            $map['a.username'] = $this->request->param('keywords');
        }
        if (!empty($this->request->param('type'))) {
            $map['l.type'] = $this->request->param('type');
        }

        $menu = self::getMenuArr();
        $list = \think\Db::table('d_admin_log')
            ->alias('l')
            ->where($map)
            ->join('d_admins a', 'l.user_id = a.id', 'LEFT')
            ->field('l.*, a.username')
            ->order('l.create_at DESC')
            ->paginate(10);

        return view('syslog', compact('menu', 'list'));
    }

    //清除缓存
    public function clear()
    {
        $data = $this->request->param();
        if(empty($data)){
            return error('请勾选缓存类型');
        }
        // dd($data);
        // if($repeat)
        // $path = glob(LOG_PATH); 
        // dump($path);exit;
        // del_files($$path);
        foreach ($data['repeat'] as $key => $value) {
            if($value == 'sys'){
                //系统缓存
                Cache::clear();
                logger(2, '清除系统缓存');

            } elseif ($value == 'log') {
                //日志缓存
                $path = glob(LOG_PATH . '*');
                foreach ($path as $item) {
                    array_map('unlink', glob($item . DS . '*.log'));
                    rmdir($item);
                }
                logger(2, '清除日志缓存');
            }  elseif ($value == 'log') {
                //模板缓存
                array_map('unlink', glob(TEMP_PATH . '/*.php'));
                logger(2, '清除模板缓存');
            }else{
                logger(2, '清除API缓存');
            }
        }
        return success(1, '清除成功');
    }


    //网站配置
    public function pc()
    {
        $menu = self::getMenuArr();
        $system = SystemModel::column('name, value'); // 同tp3的getField


        if(Request::instance()->isPost()){
            $data = $this->request->param();
            // dd($data);
            foreach ($data as $k => $v) {

                //如果上传了新的图片立刻删除原来的旧图片
                if($k == 'HOME_BANNER' && $v != $system['HOME_BANNER']){
                    @unlink(ROOT_PATH . 'public' . DS . 'uploads' . DS . $system['HOME_BANNER']);
                }
                if($k == 'CAMPAIGN_BANNER' && $v != $system['CAMPAIGN_BANNER']){
                    @unlink(ROOT_PATH . 'public' . DS . 'uploads' . DS . $system['CAMPAIGN_BANNER']);
                }
                if($k == 'NEW_BANNER' && $v != $system['NEW_BANNER']){
                    @unlink(ROOT_PATH . 'public' . DS . 'uploads' . DS . $system['NEW_BANNER']);
                }
                if($k == 'STORE_BANNER' && $v != $system['STORE_BANNER']){
                    @unlink(ROOT_PATH . 'public' . DS . 'uploads' . DS . $system['STORE_BANNER']);
                }
                if($k == 'ABOUT_BANNER' && $v != $system['ABOUT_BANNER']){
                    @unlink(ROOT_PATH . 'public' . DS . 'uploads' . DS . $system['ABOUT_BANNER']);
                }
                if($k == 'COLLE_BANNER' && $v != $system['COLLE_BANNER']){
                    @unlink(ROOT_PATH . 'public' . DS . 'uploads' . DS . $system['COLLE_BANNER']);
                }

                $str=<<<php
<?php
return array( 
//以下配置项为自动生成请勿直接修改；如需改动请在后台网站设置

// +----------------------------------------------------------------------
// | banner 设置
// +----------------------------------------------------------------------
'HOME_BANNER' =>  '{$data['HOME_BANNER']}',
'CAMPAIGN_BANNER'   =>  '{$data['CAMPAIGN_BANNER']}',
'NEW_BANNER'   =>  '{$data['NEW_BANNER']}',
'STORE_BANNER'   =>  '{$data['STORE_BANNER']}',
'ABOUT_BANNER'   =>  '{$data['ABOUT_BANNER']}',
'COPY_NAME'   =>  '{$data['COPY_NAME']}',
'WEB_NAME'   =>  '{$data['WEB_NAME']}',
'HOME_BANNER_URL'   =>  '{$data['HOME_BANNER_URL']}',
'COLLE_BANNER'   =>  '{$data['COLLE_BANNER']}',

);          
php;
            file_put_contents(CONF_PATH.'index/config.php', $str);


                SystemModel::where('name', $k)->setField('value', $v);
                $data[$k]=htmlspecialchars_decode($v);
            }

            return success(1, '保存成功');
        }
        logger(2, '编辑网站全局配置');
        
        return view('pc', compact('menu', 'system'));
    }

}
